package com.youlu.campus.admin.form;

import com.youlu.campus.admin.auth.entity.SystemRole;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.exception.BusinessException;
import com.youlu.campus.entity.VO.req.FormTaskConfigQueryVO;
import com.youlu.campus.entity.form.FormTaskConfig;
import com.youlu.campus.service.form.FormTaskConfigService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 表单基础项配置
 */
@Slf4j
@RestController
public class FormTaskConfigController {
    @Autowired
    private FormTaskConfigService formTaskConfigService;

    @Autowired
    private RoleService roleService;

    @PostMapping("/platform/form/list")
    public QResult list(@RequestBody @NotNull FormTaskConfigQueryVO req) {
        String loginName = AuthUtils.getLoginName();
        if (loginName.equals("zhouqiao") || loginName.equals("zhuhuaiqi") || loginName.equals("sujiakang")) {
            req.setCreatorId(null);
        } else {
            req.setCreatorId(AuthUtils.getUserId());
        }

        return QResult.success(formTaskConfigService.list(req));
    }

    @GetMapping("/platform/form/find-one")
    public QResult findOne(@RequestParam("id") String id) {
        return QResult.success(formTaskConfigService.findOne(id));
    }

    @PostMapping("/platform/form/update")
    public QResult update(@RequestBody @NotNull FormTaskConfig req) {
        if (!formTaskConfigService.hasPermission(AuthUtils.getUserId(), req.getId(), AuthUtils.getLoginName())) {
            log.error(":>>> 用户无操作权限:{}", AuthUtils.getUserId(), req.getId());
            throw new BusinessException("用户无操作权限");
        }
        return QResult.success(formTaskConfigService.update(req));
    }

    /**
     * 表单基础项配置 权限转移
     *
     * @param req
     * @return
     */
    @PostMapping("/platform/form/transfer")
    public QResult transfer(@RequestBody FormTaskConfig req) {
        log.info("transfer id:{}, createId:{}", req.getId(), req.getCreatorId());
        FormTaskConfig one = formTaskConfigService.findOne(req.getId());
        if (Objects.isNull(one)) {
            return QResult.fail("数据不存在");
        }
        if (!one.getCreatorId().equals(req.getCreatorId())) {
            String userId = AuthUtils.getUserId();
            List<SystemRole> roleList = roleService.findAllByUserId(userId);
            List<SystemRole> adminRole = roleList.stream().filter(r -> "admin".equals(r.getIdentifier())).collect(Collectors.toList());
            //判断是否是 管理员  或者 是不是创建者
            if (CollectionUtils.isEmpty(adminRole)) {
                return QResult.fail("你无当前活动相关权限");
            }
        }
        return QResult.success(formTaskConfigService.transferCreator(req));
    }

    @PostMapping("/platform/form/create")
    public QResult create(@RequestBody @NotNull FormTaskConfig req) {
        req.setCreatorId(AuthUtils.getUserId());
        req.setTenantId(AuthUtils.getComId());
        return QResult.success(formTaskConfigService.create(req));
    }

    @GetMapping("/platform/form/delete")
    public QResult delete(@RequestParam("id") String id) {
        if (!formTaskConfigService.hasPermission(AuthUtils.getUserId(), id, AuthUtils.getLoginName())) {
            log.error(":>>> 用户无操作权限:{}", AuthUtils.getUserId(), id);
            throw new BusinessException("用户无操作权限");
        }
        return QResult.success(formTaskConfigService.delete(id));
    }

    @GetMapping("/platform/form/copy")
    public QResult copy(@RequestParam("id") String id) {
        return QResult.success(formTaskConfigService.copy(id, AuthUtils.getUserId()));
    }

}
